<html>
	<!--
		@author Jean-Lou Dupont 
	 -->
	<head>
		<style>
		/**
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 */
.code .de1, .code .de2 {font-family: 'Courier New', Courier, monospace; font-weight: normal;}
.code  {background: #EEEEEE; border: padding: 0.2em}
.code .imp {font-weight: bold; color: red;}
.code li, .code li.li1 {font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;}
.code li.li2 {font-weight: bold;}
.code .kw1 {color: #b1b100;}
.code .kw2 {color: #000000; font-weight: bold;}
.code .kw3 {color: #000066;}
.code .co1 {color: #808080; font-style: italic;}
.code .co2 {color: #808080; font-style: italic;}
.code .coMULTI {color: #808080; font-style: italic;}
.code .es0 {color: #000099; font-weight: bold;}
.code .br0 {color: #66cc66;}
.code .sy0 {color: #66cc66;}
.code .st0 {color: #ff0000;}
.code .nu0 {color: #cc66cc;}
.code .me1 {color: #006600;}
.code .me2 {color: #006600;}
.code .re0 {color: #0000ff;}
.code .re1 {color: #ff0000}

		</style>
	</head>
	
	<body>
	
		<div id="content">
		<pre class="code" style="background: #EEEEEE; border: padding: 0.2em"><ol><li class="li1"><div class="de1"><span class="kw2">&lt;?php</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* iterator pattern</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;* This class allows for iterating a specified array</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* in a specified object instance through a ''foreach'' statement.</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* @example </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 		class Container {</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;* 			</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 			var $interesting_array;</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 		}</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 		$container = new Container;</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;* 		$iterator = new aop_iterator( $container, 'interesting_array' );</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 		foreach( $iterator as $key =&gt; $value )</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 			do_some_stuff( $key, $value );</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* </span></div></li><li class="li2"><div class="de2"><span class="coMULTI">&nbsp;* @author Jean-Lou Dupont</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* http://php-design-patterns-examples@googlecode.com/</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*/</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="kw2">class</span> VersaIterator </div></li><li class="li2"><div class="de2">	implements Iterator <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Index in source array</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li2"><div class="de2">	<span class="kw2">var</span> <span class="re1">$index</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Count of source array </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<span class="kw2">var</span> <span class="re1">$count</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2">	<span class="coMULTI">/**</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * Reference to source array</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<span class="kw2">var</span> <span class="re1">$ref</span> <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="coMULTI">/**</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">	 * Array of keys of source array</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span></div></li><li class="li1"><div class="de1">	<span class="kw2">var</span> <span class="re1">$keys</span> <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">	<span class="coMULTI">/**</span></div></li><li class="li2"><div class="de2"><span class="coMULTI">	 * Constructor</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * @param $objectContainer reference to source object</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * @param $arrayContainer name of array in source object </span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 */</span>	</div></li><li class="li2"><div class="de2">	<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span> <span class="sy0">&amp;</span><span class="re1">$objectContainer</span><span class="sy0">,</span> <span class="re1">$arrayContainer</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="co1">// pre-compute for speed</span></div></li><li class="li1"><div class="de1">		<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">ref</span> <span class="sy0">=</span> <span class="re1">$objectContainer</span><span class="sy0">-&gt;</span><span class="re1">$arrayContainer</span><span class="sy0">;</span>		</div></li><li class="li1"><div class="de1">		<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">count</span> <span class="sy0">=</span> <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">ref</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="co1">// necessary overhead as PHP does not provide</span></div></li><li class="li1"><div class="de1">		<span class="co1">// an easy way to extract 1key at the time</span></div></li><li class="li1"><div class="de1">		<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">keys</span> <span class="sy0">=</span> <a href="http://www.php.net/array_keys"><span class="kw3">array_keys</span></a><span class="br0">&#40;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">ref</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1">	<span class="coMULTI">/*********************************************************</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 * 				Iterator Interface</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">	 ********************************************************/</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <span class="kw2">function</span> <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li2"><div class="de2">		<span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">count</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <span class="kw2">function</span> <a href="http://www.php.net/current"><span class="kw3">current</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		<span class="re1">$a</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">ref</span><span class="sy0">;</span>		</div></li><li class="li1"><div class="de1">		<span class="re1">$k</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">keys</span><span class="br0">&#91;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">index</span> <span class="br0">&#93;</span><span class="sy0">;</span>		</div></li><li class="li2"><div class="de2">		<span class="kw1">return</span> <span class="br0">&#40;</span> <span class="re1">$a</span><span class="br0">&#91;</span> <span class="re1">$k</span> <span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <span class="kw2">function</span> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		<span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">keys</span><span class="br0">&#91;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">index</span> <span class="br0">&#93;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li2"><div class="de2">	<span class="kw2">public</span> <span class="kw2">function</span> <a href="http://www.php.net/next"><span class="kw3">next</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		<span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">index</span><span class="sy0">++;</span>	</div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <span class="kw2">function</span> <a href="http://www.php.net/rewind"><span class="kw3">rewind</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		<span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">index</span> <span class="sy0">=</span><span class="nu0">0</span> <span class="sy0">;</span>	</div></li><li class="li2"><div class="de2">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">	<span class="kw2">public</span> <span class="kw2">function</span> valid<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">		<span class="kw1">return</span> <span class="br0">&#40;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">index</span> <span class="sy0">&lt;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">count</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2"><span class="br0">&#125;</span><span class="co1">//end definition</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="coMULTI">/*****************************************************************</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 						EXAMPLE                                  *</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*****************************************************************/</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1"><span class="kw2">class</span> ClassWithInterestingArray <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">	<span class="kw2">var</span> <span class="re1">$interestingArray</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2">	<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">		<span class="kw1">for</span><span class="br0">&#40;</span> <span class="re1">$i</span><span class="sy0">=</span><span class="nu0">0</span><span class="sy0">;</span> <span class="re1">$i</span> <span class="sy0">&lt;</span> <span class="nu0">10</span><span class="sy0">;</span> <span class="re1">$i</span><span class="sy0">++</span> <span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">			<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">interestingArray</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st0">&quot;interesting integer: &quot;</span> <span class="sy0">.</span> <a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> </div></li><li class="li1"><div class="de1">	<span class="br0">&#125;</span></div></li><li class="li2"><div class="de2"><span class="br0">&#125;</span><span class="co1">//endclass</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="re1">$container</span> <span class="sy0">=</span> <span class="kw2">new</span> ClassWithInterestingArray<span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1"><span class="re1">$iterator</span> <span class="sy0">=</span> <span class="kw2">new</span> VersaIterator<span class="br0">&#40;</span> <span class="re1">$container</span><span class="sy0">,</span> <span class="st0">&quot;interestingArray&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1"><span class="kw1">foreach</span><span class="br0">&#40;</span> <span class="re1">$iterator</span> <span class="kw1">as</span> <span class="re1">$key</span> <span class="sy0">=&gt;</span> <span class="re1">$value</span> <span class="br0">&#41;</span></div></li><li class="li1"><div class="de1">	<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot; key: $key, value: $value <span class="es0">\n</span>&quot;</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li1"><div class="de1">__halt_compiler<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2">&nbsp;</div></li><li class="li1"><div class="de1"><span class="coMULTI">/*****************************************************************</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;* 						EXAMPLE RESULTS</span></div></li><li class="li1"><div class="de1"><span class="coMULTI">&nbsp;*****************************************************************/</span></div></li><li class="li1"><div class="de1">&nbsp;</div></li><li class="li2"><div class="de2"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">31980</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">1</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">18613</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">2</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">19274</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">3</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">19131</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">4</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">31704</span> </div></li><li class="li2"><div class="de2"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">5</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">15153</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">6</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">21014</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">7</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">22423</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">8</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">32132</span> </div></li><li class="li1"><div class="de1"> <a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">:</span> <span class="nu0">9</span><span class="sy0">,</span> value<span class="sy0">:</span> interesting integer<span class="sy0">:</span> <span class="nu0">22893</span> </div></li></ol></pre>
		</div>
	</body>

</html>
